package br.jm.mala.service.impl;

import br.jm.mala.dao.UsuarioDao;
import br.jm.mala.entity.User;
import br.jm.mala.service.LoginService;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author joao
 */
@Service("loginService")
@Transactional
public class LoginServiceImpl implements Serializable, LoginService {
    private static final long serialVersionUID = -5467169540356517670L;

    @Autowired
    private UsuarioDao userDAO;

    @Transactional(readOnly = true)
    @Override
    public User login(String username, String password) throws IllegalArgumentException {
        if (isEmptyOrNull(username) || isEmptyOrNull(password)) {
            throw new IllegalArgumentException("Atenção, username ou password vazios!");
        }
        User u = (User) userDAO.login(username, password);

        if (u == null) {
            throw new IllegalArgumentException("Erro: username ou password incorretos!");
        }
        return u;
    }

    private boolean isEmptyOrNull(String s) {
        return s == null || s.equals("");
    }

    @Override
    public void create(User entidade) {
        userDAO.create(entidade);
    }

    @Override
    public void edit(User entidade) {
        userDAO.edit(entidade);
    }

    @Override
    public void destroy(User entidade) {
        userDAO.destroy(entidade);
    }

    @Override
    public void executeUpdate(String oql) {
        userDAO.executeUpdate(oql);
    }

    @Transactional(readOnly = true)
    @Override
    public User getPorId(Object id) {
        return userDAO.getPorId(id);

    }

    @Transactional(readOnly = true)
    @Override
    public List<User> get(String oql) {
        return userDAO.get(oql);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> get(String oql, List<String> campos) {
        return userDAO.get(oql, campos);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> get(String oql, Object... campos) {
        return userDAO.get(oql, campos);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> getPorQuery(String query) {
        return userDAO.getPorQuery(query);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> getPorQuery(String query, Object... campos) {
        return userDAO.getPorQuery(query, campos);
    }

    @Transactional(readOnly = true)
    @Override
    public int getCount(String entidade) {
        return userDAO.getCount(entidade);
    }

    @Transactional(readOnly = true)
    @Override
    public int getCount(String entidade, String where) {
        return userDAO.getCount(entidade, where);
    }

    @Transactional(readOnly = true)
    @Override
    public int getCountQuery(String oql) {
        return userDAO.getCountQuery(oql);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> getNative(String cTabela, int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters, String cParametroSQL) {
        return userDAO.getNative(cTabela, first, pageSize, sortField, sortOrder, filters, cParametroSQL);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> getNative(String cTabela, int first, int pageSize, String sortField, boolean sortOrder, Map<String, String> filters, String cParametroSQL, String cFiltro) {
        return userDAO.getNative(cTabela, first, pageSize, sortField, sortOrder, filters, cParametroSQL, cFiltro);
    }
    
    @Transactional(readOnly = true)
    @Override
    public List<User> getNativeFiltroList(String cSql) {
        return userDAO.getNativeFiltroList(cSql);
    }

    /**
     *
     * @param cSql
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public User getNativeFiltro(String cSql) {
        return userDAO.getNativeFiltro(cSql);
    }

    /**
     *
     * @param oql
     * @param campos
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public User getEntidade(String oql, List<String> campos) {
        return userDAO.getEntidade(oql, campos);
    }
}
